# 启动 uvicorn api:app --reload
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import time
import random

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class Item(BaseModel):
    name: str
    description: str

def get_random_number():
    return int(random.uniform(30, 90))

def get_random_kv(index):
    return {
        "name": "城市" + str(index),
        "value": get_random_number()
    }

def get_random_kv_lat(index):
    return {
        "name": "城市" + str(index),
        "value": [int(random.uniform(100, 120)), int(random.uniform(30, 40)), get_random_number()]
    }

@app.get("/")
def get_items():
    return {
        "region_data": [
            {"name": "东北", "value": get_random_number()},
            {"name": "华东", "value": get_random_number()},
            {"name": "华北", "value": get_random_number()},
            {"name": "中南", "value": get_random_number()},
            {"name": "西南", "value": get_random_number()},
            {"name": "西北", "value": get_random_number()},
        ],
        "service_data": [
            {"name": "服务1", "value": get_random_number()},
            {"name": "服务2", "value": get_random_number()},
            {"name": "服务3", "value": get_random_number()},
            {"name": "服务4", "value": get_random_number()},
            {"name": "服务5", "value": get_random_number()},
            {"name": "服务6", "value": get_random_number()},
        ],
        "radar_data": [
            {"name": "华北", "value": get_random_number()},
            {"name": "东北", "value": get_random_number()},
            {"name": "华东", "value": get_random_number()},
            {"name": "中南", "value": get_random_number()},
            {"name": "西南", "value": get_random_number()},
            {"name": "西北", "value": get_random_number()},
        ],
        "map_data": {
            "categoryData": {
                2019: [get_random_kv(index+1) for index in range(31)],
                2020: [get_random_kv(index+1) for index in range(31)],
                2021: [get_random_kv(index+1) for index in range(31)],
                2022: [get_random_kv(index+1) for index in range(31)],
                2023: [get_random_kv(index+1) for index in range(31)],
            },
            "colors": ['#fa8c16', '#a0d911', '#13c2c2', '#1677ff', '#722ed1', '#eb2f96'],
            "topData": {
                2019: [get_random_kv_lat(index+1) for index in range(10)],
                2020: [get_random_kv_lat(index+1) for index in range(10)],
                2021: [get_random_kv_lat(index+1) for index in range(10)],
                2022: [get_random_kv_lat(index+1) for index in range(10)],
                2023: [get_random_kv_lat(index+1) for index in range(10)],
            },
            "voltageLevel": ['2019', '2020', '2021', '2022', '2023']
        }
    }
